import java.util.Arrays;

public class MyStack {
    private int[] elem;//底层是一个数组
    private int usedSize;

    public MyStack(){
        this.elem = new int[5];
    }

    public void push(int val){
        if (isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }

    public boolean isFull(){
        return usedSize == elem.length;
    }

    public int pop(){
        //1.判断栈不为空
        if (empty()){
            throw new StackEmptyException("栈为空");
        }
        //2.开始删除
        return elem[--usedSize];
    }

    public boolean empty(){
        return usedSize == 0;
    }

    public int peek(){
        //1.判断栈不为空
        if (empty()){
            throw new StackEmptyException("栈为空");
        }
        //2.开始删除
        return elem[usedSize-1];
    }

}
